package com.wang;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class TestDDZ {
    public static void main(String[] args) {
        List<String> pokep = new ArrayList<>();//用来存54张牌
        pokep.add("大王");
        pokep.add("小王");
        String[] color = {"♠","♦","♥","♣"};//定义花色
        String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};//定义牌
        for (String s : color) {
            for (String n : num) {
                pokep.add(s+n);
            }
        }
        System.out.println(pokep);//打印所有的牌
        //洗牌 把顺序打乱
        Random random = new Random();
        //进行元素交换，poke集合跟另外一个元素交换（另外一个元素索引值随机生成）
        for (int i = 0; i < pokep.size(); i++) {
            int x = random.nextInt(pokep.size());
            String t = pokep.get(i);
            pokep.set(i,pokep.get(x));
            pokep.set(x,t);
        }
        System.out.println(pokep);//乱序后|
        //定义三个玩家，进行一个发牌操作
        List<String> one = new ArrayList<>();
        List<String> two = new ArrayList<>();
        List<String> three = new ArrayList<>();
        //一个一张 最后三张底牌留给地主
            for (int i = 0; i < pokep.size(); i++) {
                String poke = pokep.get(i);
                if (i>=pokep.size() -3){
                        one.add(poke);//最后三张底牌
                }else if (i % 3 == 0){
                    one.add(poke);
                }else if (i % 3 == 1){
                    two.add(poke);
                }else{
                    three.add(poke);
            }
        }
            //打印出三个人的牌
        one.sort(new PoKeComparator());
        two.sort(new PoKeComparator() );
        three.sort(new PoKeComparator());
        System.out.println("地主牌"+one);
        System.out.println("玩家two"+two);
        System.out.println("玩家three"+three);
    }
    //给扑克牌排序定义了一个方法，实现一个比较器的接口 扑克牌是string的一个类型，用比较器通过泛型比较字符串大小
    private static class PoKeComparator implements Comparator<String>{
//重写接口的方法，自定义了一个比较的规则
        @Override
        public int compare(String o1, String o2) {
            return numOfPoKe(o1) - numOfPoKe(o2);
        }
    //定义了一个方法，让扑克牌对应成数字
        private int numOfPoKe(String poke) {
            if ("大王".equals(poke)){
                return 17;//给大王定义成 17
            }else if ("小王".equals(poke)){
                return 16;//给小王定义成 16
            }
            String numStr = poke.substring(1);//从第二位开始取值取到最后
            //更改 2 A K Q J的含义
            switch (numStr){
                case "2":
                    return 15;
                case "A":
                    return 14;
                case "K":
                    return 13;
                case "Q":
                    return 12;
                case "J":
                    return 11;
                default:// 3 - 10
                    return Integer.parseInt(numStr);
            }
        }
    }
}
